package com.zmj.framework.handler.exception;

import com.zmj.framework.exception.business.BizException;
import com.zmj.framework.exception.business.ForbiddenDataException;
import com.zmj.framework.exception.business.RepeatDataException;
import com.zmj.framework.exception.business.UnavailableDataException;
import com.zmj.framework.response.ResponseCode;
import com.zmj.framework.response.Result;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class BusinessExceptionControllerAdvice {

    @ExceptionHandler(ForbiddenDataException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleForbiddenDataException(ForbiddenDataException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.UNAVAILABLE_DATA);
    }

    @ExceptionHandler(RepeatDataException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleRepeatDataException(RepeatDataException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.REPEAT_DATA);
    }

    @ExceptionHandler(UnavailableDataException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleUnavailableDataException(UnavailableDataException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.FORBIDDEN_DATA);
    }
    @ExceptionHandler(BizException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleBizException(BizException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.BUSINESS_ERROR);
    }

}
